package com.zxx.paysdk.ui.activity

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alipay.sdk.app.EnvUtils
import com.alipay.sdk.app.PayTask
import com.zqq.baselibrary.ext.onClick
import com.zqq.baselibrary.ui.activity.BaseMvpActivity
import com.zqq.baselibrary.utils.YuanFenConverter
import com.zqq.provider.common.ProviderConstant
import com.zqq.provider.router.RouterPath
import com.zxx.paysdk.R
import com.zxx.paysdk.injection.component.DaggerPayComponent
import com.zxx.paysdk.injection.module.PayModule
import com.zxx.paysdk.presenter.PayPresenter
import com.zxx.paysdk.presenter.iview.PayView
import kotlinx.android.synthetic.main.activity_cash_register.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.toast
import org.jetbrains.anko.uiThread

/**
 * 收银台界面
 * wc
 */
@Route(path = RouterPath.PaySDK.PATH_PAY)
class CashRegisterActivity : BaseMvpActivity<PayPresenter>(), PayView, View.OnClickListener {

    //注意要添加这个：ARouter.getInstance().inject(this)
    //跳转时传递的值:Kotlin中要添加JVM
    @Autowired(name = ProviderConstant.KEY_ORDER_ID)
    @JvmField
    var mOrderId: Int = 0

    @Autowired(name = ProviderConstant.KEY_ORDER_PRICE)
    @JvmField
    var mTotalPrice: Long = 0


    override fun injectComponent() {
        DaggerPayComponent.builder()
                .activityComponent(mActivityComponent)
                .payModule(PayModule())
                .build()
                .inject(this)
        mPresenter.mView = this
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cash_register)
        //支付宝沙箱测试
        EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX)
        //初始化@Autowired注解的字段--方便Arouter传值(父类已有)
        //ARouter.getInstance().inject(this)
        initView()
        initData()
    }


    private fun initView() {
        mAlipayTypeTv.isSelected = true
        mAlipayTypeTv.onClick(this)
        mWeixinTypeTv.onClick(this)
        mBankCardTypeTv.onClick(this)
        mPayBtn.onClick(this)
    }

    //初始化数据
    private fun initData() {
        //上面已用ARouter接收
//        mOrderId = intent.getIntExtra(ProviderConstant.KEY_ORDER_ID,-1)
//        mTotalPrice = intent.getLongExtra(ProviderConstant.KEY_ORDER_PRICE,-1)

        mTotalPriceTv.text = YuanFenConverter.changeF2YWithUnit(mTotalPrice)
    }

    //点击事件
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.mAlipayTypeTv -> {
                updatePayType(true, false, false)
            }
            R.id.mWeixinTypeTv -> {
                updatePayType(false, true, false)
            }
            R.id.mBankCardTypeTv -> {
                updatePayType(false, false, true)
            }
            R.id.mPayBtn -> {
                mPresenter.getPaySign(mOrderId, mTotalPrice)
            }
        }
    }

    //选择支付类型，UI变化
    private fun updatePayType(isAliPay: Boolean, isWeiXin: Boolean, isBank: Boolean) {
        mAlipayTypeTv.isSelected = isAliPay
        mWeixinTypeTv.isSelected = isWeiXin
        mBankCardTypeTv.isSelected = isBank

    }

//    -------------------------接口返回--------------------

    //获取支付签名回调
    override fun onGetSignResult(result: String) {
        //需要在异步线程操作，Java中new Thread或者AsyTask
        doAsync {
            val resultMap: Map<String, String> = PayTask(this@CashRegisterActivity)
                    .payV2(result, true)  //result是支付宝签名
            uiThread {
                if (resultMap["resultStatus"].equals("9000")) {
                    mPresenter.payOrder(mOrderId)
                } else {
                    toast("支付失败${resultMap["memo"]}")
                }
            }
        }
    }

    //支付订单回调
    override fun onPayOrderResult(result: Boolean) {
        toast("支付成功")
        finish()
    }

}
